package com.qiche.controller.wx;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.qiche.dto.*;
import com.qiche.model.*;
import com.qiche.service.VoucherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.qiche.aspect.Result;
import com.qiche.dto.MemberCardDto;
import com.qiche.dto.MemberCardItemDto;
import com.qiche.dto.MerchantDto;
import com.qiche.dto.VoucherDto;
import com.qiche.enums.ResultEnum;
import com.qiche.model.Card;
import com.qiche.model.Merchant;
import com.qiche.model.StoreMember;
import com.qiche.service.CardService;
import com.qiche.service.MerchantService;
import com.qiche.service.StoreMemberService;
import com.qiche.service.VoucherService;
import com.qiche.vo.CardVo;

/**
 * 微信公众号C端 卡券管理控制器
 */
@RestController
@RequestMapping("/wx/card")
public class CardWxController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MerchantService merchantService;
	@Autowired
	private CardService cardService;

	@Autowired
	private StoreMemberService storeMemberService;
	@Autowired
	private VoucherService voucherService;

	/**
	 * 根据门店标识 获取会员在该门店的卡券列表
	 * 
	 * @param merchantId
	 * @param sortMoney
	 * @return
	 */
	@PostMapping("/getStoreCards")
	public Result<?> getStoreCardListByMerchantId(@RequestParam String accessToken, @RequestParam String openid,
			String unionid, @RequestParam Long merchantId, StoreMember storeMember,
			@RequestParam(required = false, defaultValue = "default") String sortMoney) {
		StoreMember member = storeMemberService.selectByKey(storeMember.getId());
		if (member == null) {
			return Result.createByErrorMessage("不存在的微信用户");
		}
		long nowTime = System.currentTimeMillis();
		//--------------优惠卷---------
		List<Voucher1Dto> voucherDtos = voucherService.selectByMerchantId(merchantId,storeMember.getId());
		//已核销优惠卷verifyVocherList
		List<Voucher1Dto> verifyVocherList = new ArrayList<>();
		List<Voucher1Dto> notVoucherVerifyList1 = new ArrayList<>();
		List<Voucher1Dto> pastVoucherList1 = new ArrayList<>();
		for (Voucher1Dto voucher1Dto:voucherDtos){
			if (voucher1Dto.getIsWriteoff()==1){
				verifyVocherList.add(voucher1Dto);
			}else if (voucher1Dto.getDueTime()==null||nowTime < voucher1Dto.getDueTime().getTime()){
				notVoucherVerifyList1.add(voucher1Dto);
			}else{
				pastVoucherList1.add(voucher1Dto);
			}
		}
		//-----------卡卷---------------
		List<MemberCardItemDto> memberCardDtos = cardService.selectMemberCardDtoByMrchantId(merchantId,
				storeMember.getPhone(), sortMoney);
		// 已核销卡券verifyList
		List<MemberCardItemDto> verifyList = memberCardDtos.stream().filter(MemberCardItemDto::isVerify)
				.collect(Collectors.toList());
		// 未核销卡券notVerifyList
		List<MemberCardItemDto> notVerifyList = memberCardDtos.stream()
				.filter(memberCardDto -> !memberCardDto.isVerify())
				.filter(memberCardDto -> nowTime < memberCardDto.getVerifyEndDate().getTime())
				.collect(Collectors.toList());
		// 过期卡券
		List<MemberCardItemDto> pastList = memberCardDtos.stream().filter(memberCardDto -> !memberCardDto.isVerify())
				.filter(memberCardDto -> nowTime > memberCardDto.getVerifyEndDate().getTime())
				.collect(Collectors.toList());
		//代金券
		MemberCardDto memberCardDto = new MemberCardDto();
		memberCardDto.setPastList(pastList);
		memberCardDto.setNotVerifyList(notVerifyList);
		memberCardDto.setVerifyList(verifyList);
		memberCardDto.setVerifyVoucherList(verifyVocherList);
		memberCardDto.setNotVoucherVerifyList(notVoucherVerifyList1);
		memberCardDto.setPastVoucherList(pastVoucherList1);

		logger.info("reslut:::{}", JSON.toJSONString(memberCardDto));
		return Result.createByResultEnum(ResultEnum.SUCCESS, memberCardDto);
	}

	/**
	 * 根据卡券id获取 门店卡券详情
	 * 
	 * @param cardId
	 * @return
	 */
	@PostMapping("/getStoreCardDetail")
	public Result<?> getStoreCardDetail(@RequestParam String accessToken,@RequestParam String openid, String unionid,
			@RequestParam Long merchantId, StoreMember storeMember, Long cardId) {

		Card card = cardService.selectById(cardId);

		MemberCardItemDto memberCardDto = new MemberCardItemDto();
		// 金额
		memberCardDto.setGetAmount(card.getCardAmount()+ "");
		// 卡券code
		memberCardDto.setCardCode(card.getCardCode());
		// 卡券id
		memberCardDto.setId(card.getId());
		// 状态(2表示已核销 1表示未核销)
		memberCardDto.setStatus(card.getStatus());
		// 卡券有效开始时间
		memberCardDto.setVerifyBeginDate(card.getVerifyBeginDate());
		// 卡券有效结束时间
		memberCardDto.setVerifyEndDate(card.getVerifyEndDate());
		// 卡卷标题
		memberCardDto.setCardTitle(card.getCardTitle());
		// 核销人
		memberCardDto.setVerifyUserName(card.getVerifyUserName());

		memberCardDto.setVerifyDate(card.getWriteoffDate());

		MerchantDto merchantDto = new MerchantDto();
		Merchant merchant = merchantService.selectById(card.getMerchantId());
		// 门店
		memberCardDto.setMerchantName(merchant.getName());
		BeanUtils.copyProperties(merchant, merchantDto);
		CardVo cardVo = new CardVo();
		cardVo.setMemberCardItemDto(memberCardDto);
		cardVo.setMerchantDto(merchantDto);
		logger.info("cardId:::{}", JSON.toJSONString(cardVo));
		return Result.createByResultEnum(ResultEnum.SUCCESS, cardVo);
	}
	@PostMapping("/getVoucherCardDetail")
	public Result<?> getVoucherCardDetail(@RequestParam String accessToken,@RequestParam String openid, String unionid,
										@RequestParam Long merchantId, StoreMember storeMember, Long cardId) {

		Voucher card = voucherService.selectByKey(cardId);

		VoucherDto voucherDto = new VoucherDto();
		// 金额
		voucherDto.setAmount(card.getAmount());
		// 卡券code
		voucherDto.setCode(card.getCode());
		// 卡券id
		voucherDto.setId(card.getId());
		// 状态(2表示已核销 1表示未核销)
		voucherDto.setIsWriteoff(card.getIsWriteoff());
		// 卡卷标题
		voucherDto.setTitle(card.getTitle());

		MerchantDto merchantDto = new MerchantDto();
		Merchant merchant = merchantService.selectById(Long.parseLong(card.getMerchantId().toString()));
		// 门店
		voucherDto.setMerchantName(merchant.getName());
		BeanUtils.copyProperties(merchant, merchantDto);
		CardVo cardVo = new CardVo();
		cardVo.setVoucherDto(voucherDto);
		cardVo.setMerchantDto(merchantDto);
		logger.info("cardId:::{}", JSON.toJSONString(cardVo));
		return Result.createByResultEnum(ResultEnum.SUCCESS, cardVo);
	}
}
